#include "config.h"
#include "MAX7219.h"
#include "GPIO.h"
#include "delay.h"

/**----------------------------------------------------------------------------------------------
  * @brief	按键初始化
  * @param  无
  * @retval 无
---------------------------------------------------------------------------------*/
void Key_Init(void)
{
	GPIO_InitTypeDef	GPIO_InitStructure;
	GPIO_InitStructure.Pin  = GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
	GPIO_InitStructure.Mode = GPIO_PullUp;//ˤɫ
	GPIO_Inilize(GPIO_P3,&GPIO_InitStructure);
}
/**----------------------------------------------------------------------------------------------
  * @brief	蜂鸣器初始化
  * @param  无
  * @retval 无
---------------------------------------------------------------------------------*/
void LED_Init(void)
{
	GPIO_InitTypeDef	GPIO_InitStructure;
	GPIO_InitStructure.Pin  = GPIO_Pin_3;
	GPIO_InitStructure.Mode = GPIO_OUT_PP;
	GPIO_Inilize(GPIO_P3,&GPIO_InitStructure);
}
/**----------------------------------------------------------------------------------------------
  * @brief	按键扫描读取
  * @param  无
  * @retval 无
---------------------------------------------------------------------------------*/
unsigned char KeyNum(void)
{
	unsigned char Keynum = 0;
	if(P34 == 0){delay_ms(20);while(P34 == 0);delay_ms(20);Keynum=1;}
	if(P35 == 0){delay_ms(20);while(P35 == 0);delay_ms(20);Keynum=2;}
	if(P36 == 0){delay_ms(20);while(P36 == 0);delay_ms(20);Keynum=3;}
	if(P37 == 0){delay_ms(20);while(P37 == 0);delay_ms(20);Keynum=4;}
	return Keynum;
}

/**----------------------------------------------------------------------------------------------
  * @brief	MAX7219GPIO初始化
  * @param  无
  * @retval 无
---------------------------------------------------------------------------------*/
void GPIO_Init(void)
{
	GPIO_InitTypeDef	GPIO_InitStructure;
	GPIO_InitStructure.Pin  = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_6|GPIO_Pin_7;
	GPIO_InitStructure.Mode = GPIO_OUT_PP;
	GPIO_Inilize(GPIO_P1,&GPIO_InitStructure);
}

/**----------------------------------------------------------------------------------------------
  * @brief  基本函数，写入一个8位数据0xXX 
  * @param  temp：写入的字节
  * @retval 无
---------------------------------------------------------------------------------*/
void Max7219_Wbyte(unsigned char temp)
{
	unsigned char i;
	for (i=0;i<8;i++)
	{
		MySPI_SCL = 0;
		MySPI_SDA = temp&(0x80>>i);
		MySPI_SCL = 1;
	}
}
/**----------------------------------------------------------------------------------------------
  * @brief	写入命令
  * @param  无
  * @retval 无
---------------------------------------------------------------------------------*/
void Max7219_WCmd(unsigned char Cmd,unsigned char DATA)
{
	MySPI_CS=0;
	Max7219_Wbyte(Cmd);
	Max7219_Wbyte(DATA);
	MySPI_CS=1;
}

/**----------------------------------------------------------------------------------------------
  * @brief	MAX7219初始化
  * @param  无
  * @retval 无
---------------------------------------------------------------------------------*/
void MAX7019_Init(void)     
{
	GPIO_Init();
	Max7219_WCmd(SHUT_DOWN, 0x01);   	//正常运行:0xx1 关机模式:0xx0
	Max7219_WCmd(DISPLAY_TEST, 0x00); 	//正常运行:0xx0 显示测试模式:
	Max7219_WCmd(DECODE_MODE, 0xFF);   	//解码模式选择 BOS码
	Max7219_WCmd(SCAN_LIMIT, 0x04);   	//扫描限制:0~7 0xX0~0xX7
	Max7219_WCmd(INTENSITY, 0x0F);   	//亮度选择0xX0~0xXF
}

/**----------------------------------------------------------------------------------------------
  * @brief	MAX7219重置显示数值
  * @param  无
  * @retval 无
---------------------------------------------------------------------------------*/
void MAX7019_Col(void)
{
	unsigned char i;
	for(i=0;i<8;i++)
    {
		Max7219_WCmd(i+1,0xFF);
    }
}